<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Advertisement;
use App\Models\Advertisement as AdvertisementModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;

class AdvertisementController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Advertisement(), function (Grid $grid) {
            $grid->id->sortable();
            $grid->title;
            $grid->location->display(function ($value) {
                return AdvertisementModel::$locationMap[$value];
            });
            $grid->is_title_show->display(function ($value) {
                return $value ? "是" : "否";
            });
            $grid->cover->image('', 200);
            $grid->start_time;
            $grid->end_time;
            $grid->jump_type->display(function ($value) {
                return AdvertisementModel::$jumpTypeMap[$value];
            });
            $grid->sort;
            $grid->status->display(function ($value) {
                return $value ? "开启" : "禁用";
            });
            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->actions(function ($actions) {
               $actions->disableView();
            });
        
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
        
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Advertisement(), function (Form $form) {
            $form->display('id');
            $form->text('title')->required();
            $form->select('location')->options(AdvertisementModel::$locationMap)->required()->help('轮播图才会进行循环滚动，其他的就算添加了多条，只会在有效时间内取第一条');
            $form->radio('is_title_show')->options([0 => '否', 1 => '是'])->default(1);
            $form->image('cover')->required()->help("首页轮播建议大小： 高400px * 宽450px<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;普通广告建议大小： 高100px * 宽400px");
            $form->datetime('start_time')->required();
            $form->datetime('end_time')->required();
            $form->text('jump_link')->required()->help('请填写对应的id, 不填写则不进行跳转');
            $form->select('jump_type')->options(AdvertisementModel::$jumpTypeMap);
            $form->number('sort')->default(100);
            $form->radio('status')->options([0 => '禁用', 1 => '开启'])->default(1);
            $form->textarea('silder_text');
        
            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
